<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix" style="text-align: center;line-height: 30px">
        <h3>用户登录</h3>
      </div>
      <div class="text item">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-row>
            <el-form-item label="账号" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
          </el-row>
          <br>
          <el-row>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
          </el-row>
          <br>
          <el-row>
            <div style="text-align: center">
              <el-form-item>
                <el-button type="primary" @click="userregister">登录</el-button>
                <el-button @click="cancel">取消</el-button>
              </el-form-item>
            </div>
          </el-row>
        </el-form>
      </div>
    </el-card>

  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Register",
  data() {
    return {
      ruleForm: {
        name: '',
        pass: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    userregister() {
      let url = 'apj/login/userLogin/'+this.ruleForm.name+'/'+this.ruleForm.pass
      this.$axios.get(url).then(r=>{
        // eslint-disable-next-line no-unused-vars
        let user = r.data
        if (user != null && user != 'null'){
          sessionStorage.setItem("user",JSON.stringify(user))
          this.$message.success("登录成功")
          this.$emit("getuser")
          this.$emit("init")
          this.$emit("close")
        }else{
          this.ruleForm.name=''
          this.ruleForm.pass=''
          this.$message.success("账号或密码错误!")
        }
      })
      let url1 = 'apj/log/insertLog/'+this.ruleForm.name
      this.$axios.get(url1).then(r=>{
        console.log(r.data)
      })
    },
    cancel(){
      this.ruleForm.name=''
      this.ruleForm.pass=''
    }
  }
}
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 600px;
}
</style>